package com.example.myui;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.myui.activity.AsyncActivity;
import com.example.myui.activity.BindServiceActivity;
import com.example.myui.activity.BottomNavActivity;
import com.example.myui.activity.BottomnavBottonActivity;
import com.example.myui.activity.CustomActivity;
import com.example.myui.activity.DialogActivity;
import com.example.myui.activity.GalleryActivity;
import com.example.myui.activity.GlideActivity;
import com.example.myui.activity.GlideCacheActivity;
import com.example.myui.activity.HandlerActivity;
import com.example.myui.activity.IntentServiceActivity;
import com.example.myui.activity.KotlinActivity;
import com.example.myui.activity.LazyloadActivity;
import com.example.myui.activity.MultiDownloadActivity;
import com.example.myui.activity.NotifyUtilActivity;
import com.example.myui.activity.PickerActivity;
import com.example.myui.activity.StatusActivity;
import com.example.myui.activity.UIActivity;
import com.example.myui.activity.WebActivity;
import com.example.myui.activity.ZBarActivity;
import com.example.myui.activity.permission.PermissionActivity;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private int REQUEST_CODE_CHOOSE = 1001;
    private List<Uri> mSelected = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_kot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, KotlinActivity.class));
            }
        });
        //handler
        findViewById(R.id.btn_handler).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, HandlerActivity.class));
            }
        });

        //bindservice
        findViewById(R.id.btn_bindservice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,BindServiceActivity.class));
            }
        });
    }

    //常见ui
    public void doUI(View view) {
        startActivity(new Intent(MainActivity.this, UIActivity.class));
    }

    //permission
    public void doPermission(View view) {
        startActivity(new Intent(MainActivity.this, PermissionActivity.class));
    }

    //沉浸式
    public void doStatusBar(View view) {
        startActivity(new Intent(MainActivity.this, StatusActivity.class));
    }


    //仿知乎照片选择
    @SuppressLint("CheckResult")
    public void doZhiHu(View view) {
//        startActivity(new Intent(MainActivity.this,ZhiHuActivity.class));
        RxPermissions rxPermissions = new RxPermissions(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            rxPermissions.requestEach(Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(permission -> {
                        if (permission.granted) {
                            Log.i(TAG, "doZhiHu: " + permission.granted);
                            Matisse.from(this)
                                    .choose(MimeType.allOf())
                                    .countable(true)
                                    .theme(R.style.Matisse_Dracula)
                                    .maxSelectable(9)
                                    .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                                    .thumbnailScale(0.85f)
                                    .imageEngine(new GlideEngine())
                                    .forResult(REQUEST_CODE_CHOOSE);

                        } else if (permission.shouldShowRequestPermissionRationale) {
                            Log.i(TAG, "doZhiHu: " + permission.name);
                            //有的权限点了不在询问
                        } else {
                            Log.i(TAG, "doZhiHu: ");
                            //当点击了不在询问之后 在此点击的时候
                            //去设置界面 设置权限

                        }

                    });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE) {
            if (resultCode == RESULT_OK) {
                mSelected = Matisse.obtainResult(data);
                for (int i = 0; i < mSelected.size(); i++) {
                    Log.i(TAG, "onActivityResult: " + mSelected.get(i).toString());
                }
            }
        }
    }

    //    gilde 4.4.0
    public void doGlide(View view) {
        startActivity(new Intent(MainActivity.this, GlideActivity.class));
    }

    //    Zbar 二维码
    public void doZbar(View view) {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.CAMERA)
                .subscribe(permission -> {
                    if (permission.granted) {
                        startActivity(new Intent(MainActivity.this, ZBarActivity.class));
                    } else {
                        Toast.makeText(MainActivity.this, "去设置camera", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public void doNav(View view) {
        startActivity(new Intent(MainActivity.this, BottomNavActivity.class));
    }

    public void doNavBotton(View view) {
        startActivity(new Intent(MainActivity.this, BottomnavBottonActivity.class));
    }

    public void doMultiDownload(View view) {
        requestPermission();
    }

    private void requestPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Observer<Permission>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Permission permission) {
                        if (permission.granted) {
                            startActivity(new Intent(MainActivity.this, MultiDownloadActivity.class));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @SuppressLint("InlinedApi")
    public void doGlallery(View view) {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(permission -> {
                    if (permission.granted) {
                        startActivity(new Intent(MainActivity.this, GalleryActivity.class));
                    } else {
                        Toast.makeText(MainActivity.this, "去设置camera", Toast.LENGTH_SHORT).show();
                    }
                });

    }

    public void doPicker(View view) {
        startActivity(new Intent(MainActivity.this, PickerActivity.class));
    }

    public void doCustomView(View view) {
        startActivity(new Intent(MainActivity.this, CustomActivity.class));
    }

    public void doIntentService(View view) {
        startActivity(new Intent(MainActivity.this, IntentServiceActivity.class));
    }

    //    android JS webview 安卓JS
    public void doAndroid_JS(View view) {
//        startActivity(new Intent(MainActivity.this,AgentWebActivity.class ));
        startActivity(new Intent(MainActivity.this, WebActivity.class));
    }

    //通知类
    public void onNotifytion(View view) {
        startActivity(new Intent(MainActivity.this, NotifyUtilActivity.class));
    }

    public void doShowDialog(View view) {
        startActivity(new Intent(MainActivity.this, DialogActivity.class));
    }

    //    viewpager tablayout lazyload
    public void doNavBotton2(View view) {
        startActivity(new Intent(MainActivity.this, LazyloadActivity.class));
    }

    //glide加载图片 设置缓存
    public void goGlidechahe(View view) {
        startActivity(new Intent(MainActivity.this, GlideCacheActivity.class));
    }

    public void doAsync(View view) {
        startActivity(new Intent(MainActivity.this,AsyncActivity.class));
    }
}
